﻿package demo.util {
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObject;
	import flash.display.StageAlign;
	
	public class DisplayObjectContainerUtil {

		/**
		 * remove all children
		 */
		public static function removeAllChildren(container:DisplayObjectContainer):void {
			removeAllChildrenBut(container, null);
		}

		/**
		 * remove all child just exclude the specifical child.
		 */
		public static function removeAllChildrenBut(container:DisplayObjectContainer, child:DisplayObject):void {
			for (var i:int = 0; i < container.numChildren; i++) {
				if (container.getChildAt(i) != child) {
					container.removeChildAt(i);
				}
			}
		}

		/**
		 * remove all child just exclude the buttom child.
		 */
		public static function removeAllChildrenButButtom(container:DisplayObjectContainer):void {
			var target:DisplayObject = container.getChildAt(0);
			removeAllChildrenBut(container, target);
		}

		/**
		 * remove all child just exclude the top child.
		 */
		public static function removeAllChildrenButTop(container:DisplayObjectContainer):void {
			var target:DisplayObject = container.getChildAt(container.numChildren - 1);
			removeAllChildrenBut(container, target);
		}

		/**
		 *
		 */
		public static function hideAllChildrenBut(container:DisplayObjectContainer, child:DisplayObject):void {
			DisplayObjectContainerUtil.hideAllChildren(container);
			child.visible = true;
		}

		/**
		 *
		 */
		public static function hideAllChildren(container:DisplayObjectContainer):void {
			for (var i:int = 0; i < container.numChildren; i++) {
				container.getChildAt(i).visible = false;
			}
		}

		/**
		 *
		 */
		public static function showAllChildren(container:DisplayObjectContainer):void {
			for (var i:int = 0; i < container.numChildren; i++) {
				container.getChildAt(i).visible = true;
			}
		}

		
		/**
		 *
		 */
		public static function layoutChild(target:DisplayObject, container:DisplayObjectContainer, mode:String = null):void {
			
			switch (mode) {
				case StageAlign.TOP_LEFT:
					break;
				case StageAlign.TOP:
					target.x = (container.width - target.width) / 2;
					target.y = 0;
					break;
				case StageAlign.TOP_RIGHT:
					break;
				case StageAlign.RIGHT:
					break;
				case StageAlign.BOTTOM_RIGHT:
					break;
				case StageAlign.BOTTOM:
					break;
				case StageAlign.BOTTOM_LEFT:
					break;
				case StageAlign.LEFT:
					break;
				default:
					target.x = (container.width - target.width) / 2;
					target.y = (container.height - target.height) / 2;
					break;
			}
		}
		
	}
}